package com.example.bootdemo.listeners;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Map;
import java.util.Set;

public class MyAppEnvPreparedListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

    private static Logger logger = LoggerFactory.getLogger(MyAppEnvPreparedListener.class);

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) {
        logger.info("监听到ApplicationEnvironmentPreparedEvent事件......");

        String className = applicationEnvironmentPreparedEvent.getSource().getClass().getName();

        logger.info("className={}",className);

        ConfigurableEnvironment environment = applicationEnvironmentPreparedEvent.getEnvironment();

        String[] activeProfiles = environment.getActiveProfiles();

        for(int i=0;i<activeProfiles.length;i++){
            logger.info("activeProfiles["+i+"]="+activeProfiles[i]);
        }

        Map<String, Object> systemProperties = environment.getSystemProperties();

        Set<String> keySet = systemProperties.keySet();

        for(String key: keySet){
            logger.info(key+"="+systemProperties.get(key));
        }

    }
}
